package com.hsd.znsh.controller.app;

import com.hsd.znsh.entity.Sensor;
import com.hsd.znsh.entity.SensorData;
import com.hsd.znsh.entity.SensorType;
import com.hsd.znsh.entity.User;
import com.hsd.znsh.enums.ResultEnum;
import com.hsd.znsh.exception.ZnshException;
import com.hsd.znsh.pojo.output.HostGeolocationVO;
import com.hsd.znsh.pojo.output.HostVO;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.pojo.output.SensorVO;
import com.hsd.znsh.pojo.query.SensorInfoQuery;
import com.hsd.znsh.service.*;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-05-02 10:21:49
 */
@RestController
@RequestMapping("/app/sensor")
public class APPSensorController {

    @Autowired
    private SensorDataService sensorDataService;

    @Autowired
    private SensorService sensorService;

    @Autowired
    private SensorTypeService sensorTypeService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private FogIdService fogIdService;

    @GetMapping("/getSensorTypeListByHostSn")
    public ResultVO getSensorTypeListByHostSn(String hostSn){
        List<SensorType> list=sensorTypeService.getSensorTypeListByHostSn(hostSn);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getSensorAverageValueByHostSn/{bh}")
    public ResultVO getSensorAverageValueByHostSn(String hostSn,Integer sensorTypeId,
                                                  @PathVariable("bh") Integer bh){
        Map<String,Object> value=sensorService.getSensorAverageValueByHostSn(hostSn,sensorTypeId,bh);
        return ResultVOUtil.success(value);
    }

    @GetMapping("/getSensorListByHostSn")
    public ResultVO getSensorListByHostSn(String hostSn){
        List<Sensor> list=sensorService.getSensorListByHostSn(hostSn);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getSensorTypeBySensorSn")
    public ResultVO getSensorTypeBySensorSn(String sensorSn){
        SensorType sensorType=sensorService.getSensorTypeBySensorSn(sensorSn);
        return ResultVOUtil.success(sensorType);
    }

    @GetMapping("/getSensorData")
    public ResultVO getSensorData(String sensorSn,Integer bh){
        Map<String,Object>  sensorData=sensorDataService.getSensorData(sensorSn,bh);
        return ResultVOUtil.success(sensorData);
    }

    @GetMapping("/getSensorHistoryData")
    public ResultVO getSensorHistoryInfo(@Valid SensorInfoQuery sensorInfoQuery, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        List<SensorData> list=sensorDataService.getSensorHistoryData(sensorInfoQuery);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getSensorAliasListByHostSn")
    public ResultVO getSensorAliasListByHostSn(String hostSn){
        List<Sensor> list=sensorService.getSensorListByHostSn(hostSn);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getSensorInfoBySensorSn")
    public ResultVO getSensorInfoBySensorSn(String sensorSn){
         return ResultVOUtil.success(sensorService.getSensorInfoBySensorSn(sensorSn));
    }

    @GetMapping("/getSensorTypeList")
    public ResultVO getSensorTypeList(){
        List<SensorType> list=sensorTypeService.getAllSensorTypeList();
        return ResultVOUtil.success(list);
    }

    @PostMapping("/editSensor")
    public ResultVO editSensor(@Valid Sensor sensor,BindingResult bindingResult,
                               @RequestHeader("token") String token,String fogId,String signatrue){
        if(!fogIdService.verify(fogId,signatrue)) throw new ZnshException(ResultEnum.FOGID_ERROR);
        ValidatorUtil.valid(bindingResult);
        User user=redisService.get(token,User.class);
        if(sensor.getId()==null){
            return ResultVOUtil.success(sensorService.addSensorByAppUser(sensor,user));
        }else{
            return ResultVOUtil.success(sensorService.updateSensorByAppUser(sensor,user));
        }
    }

    @PostMapping("/deleteSensor")
    public ResultVO deleteSensor(String sensorSn){
        sensorService.deleteSensor(sensorSn);
        return ResultVOUtil.success("删除成功");
    }

    @GetMapping("/getSensorListByAppUser")
    public ResultVO getHostGeoLocation(@RequestHeader("token") String token){
        User user=redisService.get(token,User.class);
        List<Sensor> list=sensorService.getSensorListByAppUser(user);
        return ResultVOUtil.success(list);
    }
}
